package com.luoxue.service;

import com.luoxue.entity.CommonValidator;
import com.luoxue.entity.ParameterValidation;
import com.luoxue.mapper.CommonValidatorMapper;
import com.luoxue.mapper.ParameterValidationMapper;
import com.luoxue.util.MyStringUtil;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.regex.Pattern;

@Service
public class ParameterValidationService {

    @Resource
    ParameterValidationMapper parameterValidationMapper;

    @Resource
    CommonValidatorMapper commonValidatorMapper;


    public List<CommonValidator> parameterValidate(String sqlTag, Map<String, Object> paramMap) {
        List<CommonValidator> validationMsgList = new ArrayList<>();
        List<ParameterValidation> parameterValidationList = parameterValidationMapper.selectBySqlTag(sqlTag);
        if (parameterValidationList != null && !parameterValidationList.isEmpty()) {
            Set<String> regexTypes = new HashSet<>();
            parameterValidationList.forEach(parameterValidation -> {
                List<CommonValidator> validatorList = parameterValidation.getRegexRulesList();
                if (validatorList != null) {
                    validatorList.forEach(validator -> {
                        String regexType = validator.getRegexType();
                        if (!isCustomRegexType(regexType)) {
                            regexTypes.add(regexType);
                        }
                    });
                }
            });
            List<CommonValidator> commonValidatorList = commonValidatorMapper.selectByRegexTypes(regexTypes);
            parameterValidationList.forEach(parameterValidation -> {
                String paramName = parameterValidation.getParamName();
                Object paramValueO = paramMap.get(paramName);
                if (paramValueO == null) {
                    validationMsgList.add(new CommonValidator(paramName, null, "参数为空"));
                    return;
                }
                String paramValue = String.valueOf(paramValueO);

                List<CommonValidator> validatorList = parameterValidation.getRegexRulesList();
                if (validatorList != null) {
                    validatorList.forEach(validator -> {
                        String regexType = validator.getRegexType();
                        CommonValidator validatorMsg = null;
//                        System.out.println(paramName + " -> " + regexType + " -> " + paramValue);
                        if (isCustomRegexType(regexType)) {
                            if (!Pattern.matches(validator.getRegexText(), paramValue)) {
                                validatorMsg = new CommonValidator(paramName, regexType, MyStringUtil.getValue(validator.getErrorMsg()));
                            }
                        } else {
                            Optional<CommonValidator> optional = commonValidatorList.stream().dropWhile(cValidator -> !cValidator.getRegexType().equals(regexType)).findFirst();
                            if (optional.isPresent()) {
                                CommonValidator commonValidator = optional.get();
                                if (!Pattern.matches(commonValidator.getRegexText(), paramValue)) {
                                    validatorMsg = new CommonValidator(paramName, regexType, MyStringUtil.getValue(MyStringUtil.getValue(commonValidator.getErrorMsg(), validator.getErrorMsg())));
                                }
                            } else {
                                validatorMsg = new CommonValidator(paramName, regexType, "未匹配到校验规则");
                            }
                        }

                        if (validatorMsg != null) {
                            validationMsgList.add(validatorMsg);
                        }
                    });
                }
            });
        }

        return validationMsgList;
    }


    private Boolean isCustomRegexType(String regexType) {
        return Strings.isNotBlank(regexType) && regexType.equals("custom");
    }

}
